package com.campus.secondhand.mapper;

import java.util.List;

import com.campus.secondhand.entity.Messages;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Delete;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

/**
 * 消息Mapper接口
 */
public interface MessageMapper extends BaseMapper<Messages> {

        /**
         * 查找两个用户之间的所有消息
         * 
         * @param userAId 用户A ID
         * @param userBId 用户B ID
         * @return 消息列表
         */
        @Select("SELECT * FROM messages WHERE (sender_id = #{userAId} AND receiver_id = #{userBId}) OR (sender_id = #{userBId} AND receiver_id = #{userAId}) ORDER BY created_at ASC")
        List<Messages> findMessagesBetweenUsers(@Param("userAId") Long userAId, @Param("userBId") Long userBId);

        /**
         * 查找与特定商品相关的用户间消息
         * 
         * @param userAId   用户A ID
         * @param userBId   用户B ID
         * @param productId 商品ID
         * @return 消息列表
         */
        @Select("SELECT * FROM messages WHERE ((sender_id = #{userAId} AND receiver_id = #{userBId}) OR (sender_id = #{userBId} AND receiver_id = #{userAId})) AND product_id = #{productId} ORDER BY created_at ASC")
        List<Messages> findMessagesBetweenUsersForProduct(@Param("userAId") Long userAId,
                        @Param("userBId") Long userBId,
                        @Param("productId") Long productId);

        /**
         * 将用户的所有未读消息标记为已读
         * 
         * @param senderId   发送者ID
         * @param receiverId 接收者ID
         * @return 更新的记录数
         */
        @Update("UPDATE messages SET is_read = 1 WHERE sender_id = #{senderId} AND receiver_id = #{receiverId} AND is_read = 0")
        int markMessagesAsRead(@Param("senderId") Long senderId, @Param("receiverId") Long receiverId);

        /**
         * 获取用户的未读消息数
         * 
         * @param receiverId 接收者ID
         * @return 未读消息数
         */
        @Select("SELECT COUNT(*) FROM messages WHERE receiver_id = #{receiverId} AND is_read = 0")
        int countUnreadMessages(@Param("receiverId") Long receiverId);

        /**
         * 获取指定用户间的最后一条消息
         * 
         * @param userAId 用户A ID
         * @param userBId 用户B ID
         * @return 最后一条消息
         */
        @Select("SELECT * FROM messages WHERE (sender_id = #{userAId} AND receiver_id = #{userBId}) OR (sender_id = #{userBId} AND receiver_id = #{userAId}) ORDER BY created_at DESC LIMIT 1")
        Messages findLastMessageBetweenUsers(@Param("userAId") Long userAId, @Param("userBId") Long userBId);

        /**
         * 根据会话ID获取消息列表，支持分页
         */
        @Select("SELECT m.* FROM messages m " +
                        "JOIN conversations c ON " +
                        "(m.sender_id = c.user_a_id AND m.receiver_id = c.user_b_id) OR " +
                        "(m.sender_id = c.user_b_id AND m.receiver_id = c.user_a_id) " +
                        "WHERE c.id = #{conversationId} " +
                        "ORDER BY m.created_at DESC " +
                        "LIMIT #{offset}, #{pageSize}")
        List<Messages> findMessagesByConversationId(@Param("conversationId") Long conversationId,
                        @Param("offset") Integer offset, @Param("pageSize") Integer pageSize);

        /**
         * 删除两个用户之间的所有消息
         * 
         * @param userAId 用户A ID
         * @param userBId 用户B ID
         * @return 删除的记录数
         */
        @Delete("DELETE FROM messages WHERE (sender_id = #{userAId} AND receiver_id = #{userBId}) OR (sender_id = #{userBId} AND receiver_id = #{userAId})")
        int deleteMessagesBetweenUsers(@Param("userAId") Long userAId, @Param("userBId") Long userBId);
}